package com.nuyoah.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.nuyoah.common.dto.LoginDto;
import com.nuyoah.common.dto.UserDto;
import com.nuyoah.entity.Student;
import com.nuyoah.entity.User;
import com.nuyoah.mapper.StudentMapper;
import com.nuyoah.mapper.UserMapper;
import com.nuyoah.service.StudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 存储学生的特定信息 服务实现类
 * </p>
 *
 * @author xyx
 * @since 2024-06-03
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public Student getByUserId(Integer id) {
        return studentMapper.selectByUserId(id);
    }

    @Override
    public UserDto login(LoginDto loginDto) {
        User student = userMapper.getUserByUsername(loginDto.getUsername());
        if (student == null) {
            return null;
        }
        StpUtil.login(student.getId());

        Student s = studentMapper.getByUserId(student.getId());
        UserDto userDto = new UserDto();
        userDto.setId(student.getId());
        userDto.setUsername(student.getUsername());
        userDto.setAge(student.getAge());
        userDto.setGender(student.getGender());
        userDto.setPhone(student.getPhone());
        userDto.setEmail(student.getEmail());
        userDto.setSClass(s.getSClass());
        userDto.setLearningHistory(s.getLearningHistory());
        userDto.setSchool(s.getSchool());

        userDto.setToken(StpUtil.getTokenValue());

        return userDto;
    }

}
